package com.chujian.util;

import java.lang.reflect.Type;
import java.util.Map;

import com.chujian.model.Book;
import com.chujian.model.Food;
import com.chujian.model.FoodList;
import com.chujian.model.Order;
import com.chujian.model.Table;
import com.chujian.model.User;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
/**
 * json数据解析的类
 * @author cc
 *
 */
public class JsonUtil {
/**
 * 对json串进行解析，返回order对象
 * @param jsondata
 * @return Order
 */
	public static Order OrderFromJson(String jsondata) {

		Type orderType = new TypeToken<Order>() {}.getType();
		Gson gson = new Gson();
		Order order = gson.fromJson(jsondata, orderType);
		return order;

	}
	/**
	 * 对json串进行解析
	 * @param jsondata
	 * @return User
	 */
	public static User UserFromJson(String jsondata){
		
		Type userType=new TypeToken<User>(){}.getType();
		Gson gson=new Gson();
		User user=gson.fromJson(jsondata, userType);
		return user;
	}
	public static Food FoodFromJson(String jsondata){
		Type type=new TypeToken<Food>(){}.getType();
		Gson gson=new Gson();
		Food food=gson.fromJson(jsondata, type);
		return food;
	}
	public static Table TableFromJson(String jsondata){
		Type type=new TypeToken<Table>(){}.getType();
		Gson gson=new Gson();
		Table table=gson.fromJson(jsondata, type);
		return table;
	}
	/**
	 * 预订订单解析
	 * @param jsondata
	 * @return
	 */
	public static Book BookFromJson(String jsondata){
		Type type=new TypeToken<Book>(){}.getType();
		Gson gson=new Gson();
		Book book=gson.fromJson(jsondata, type);
		return book;
	}
	
	public static FoodList FoodListFromJson(String jsondata){
		Type type=new TypeToken<FoodList>(){}.getType();
		Gson gson=new Gson();
		FoodList foodlist=gson.fromJson(jsondata, type);
		return foodlist;
	}
	
	public static Map<String,Long> MapSLFromJson(String jsondata){
		Type type=new TypeToken<Map<String,Long>>(){}.getType();
		Gson gson=new Gson();
		Map<String,Long> map=gson.fromJson(jsondata, type);
		return map;
	}
}
